﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HomeStayWebCP.Models;
using HomeStayWebCP.Models.BusinessObject;
using DAL;

namespace HomeStayWebCP.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/
        public static SearchPostBO houseSt;
        //  static string _txtsearchstatic;
        public ActionResult IndexMap(int province, int district, string txtsearch)
        {
            SearchPostBO searchBo = new SearchPostBO();
            //Get ProvinceID for showing in combobox
            RegionModel model = new RegionModel();
            List<Region> listprovince = model.SelectProvince();
            List<SelectListItem> listitem1 = new List<SelectListItem>();
            foreach (var reg in listprovince)
            {
                listitem1.Add(new SelectListItem
                {
                    Text = reg.Name,
                    Value = reg.RegionID.ToString()
                });
            }

            ViewBag.Province = listitem1;
            //Get Type of House
            TypeOfHouseModel typehousemodel = new TypeOfHouseModel();
            List<TypeOfHous> listtype = typehousemodel.GetTypeofHouse();
            List<SelectListItem> listitem2 = new List<SelectListItem>();
            foreach (var type in listtype)
            {
                listitem2.Add(new SelectListItem
                {
                    Text = type.Name,
                    Value = type.TypeOfHouseID.ToString()
                });
            }
            ViewBag.TypeofHouse = listitem2;
            //Get type of payment
            var pricetypes = new List<SelectListItem>();
            pricetypes.Add(new SelectListItem { Value = "-1", Text = "Select-PriceType", Selected = true });
            pricetypes.Add(new SelectListItem { Value = "1", Text = "Perdate" });
            pricetypes.Add(new SelectListItem { Value = "2", Text = "Perweek" });
            pricetypes.Add(new SelectListItem { Value = "3", Text = "Permonth" });
            ViewBag.pricetypes = pricetypes;

            ////Get Homestay type
            HomeStayTypeModel homestaytype = new HomeStayTypeModel();
            List<HomeStayType> homestaytypes = homestaytype.GetHomeStayType();
            List<SelectListItem> checklist = new List<SelectListItem>();
            foreach (var item in homestaytypes)
            {
                checklist.Add(new SelectListItem
                {
                    Text = item.Name,
                    Value = item.HomeStayTypeID.ToString()
                });
            }
            ViewBag.HomeStayTypeID = checklist;
            //set default min price
            searchBo.MinPrice = 1;
            searchBo.MaxPrice = 501;
            searchBo.MinArea = 0;
            searchBo.MaxArea = 201;
            if (province != 0)
            {
                searchBo.ProvinceID = province;
                RegionModel modelregion = new RegionModel();
                List<Region> listdistrict = model.SelectDistrict(province);
                List<SelectListItem> listitem3 = new List<SelectListItem>();
                if (listdistrict.Count > 0)
                {
                    listitem3.Add(new SelectListItem
                    {
                        Text = "All District",
                        Value = "-1"
                    });
                    foreach (var dis in listdistrict)
                    {

                        listitem3.Add(new SelectListItem
                        {
                            Text = dis.Name,
                            Value = dis.RegionID.ToString()
                        });
                    }

                }
                else
                {
                    listitem3.Add(new SelectListItem
                    {
                        Text = "Select District",
                        Value = "-1"
                    });

                }
                ViewBag.District = listitem3;
                if (district != 0)
                {
                    searchBo.DistrictID = district;
                }
                ViewBag.Homepagepass = "SearchMap";
            }
            ViewBag.txtSearch = txtsearch;
            HttpContext.Session["txtSearchTemp"] = txtsearch;
            return View("Index", searchBo);
        }
        //Try another Index
        [ValidateInput(false)]
        public ActionResult Index(string txtsearch)
        {
            SearchPostModel searchdatamodel = new SearchPostModel();
            var provincedistricID = searchdatamodel.GetProDisId(txtsearch);
            int? provinceId = provincedistricID.ProvinceID;
            int? districtId = provincedistricID.DistrictID;
            SearchPostBO searchBo = new SearchPostBO();
            //Get ProvinceID for showing in combobox
            RegionModel model = new RegionModel();
            List<Region> listprovince = model.SelectProvince();
            List<SelectListItem> listitem1 = new List<SelectListItem>();
            foreach (var reg in listprovince)
            {
                listitem1.Add(new SelectListItem
                                  {
                                      Text = reg.Name,
                                      Value = reg.RegionID.ToString()
                                  });
            }

            ViewBag.Province = listitem1;
            //Get Type of House
            TypeOfHouseModel typehousemodel = new TypeOfHouseModel();
            List<TypeOfHous> listtype = typehousemodel.GetTypeofHouse();
            List<SelectListItem> listitem2 = new List<SelectListItem>();
            foreach (var type in listtype)
            {
                listitem2.Add(new SelectListItem
                                  {
                                      Text = type.Name,
                                      Value = type.TypeOfHouseID.ToString()
                                  });
            }
            ViewBag.TypeofHouse = listitem2;
            //Get type of payment
            var pricetypes = new List<SelectListItem>();
            pricetypes.Add(new SelectListItem {Value = "-1", Text = "Select-PriceType", Selected = true});
            pricetypes.Add(new SelectListItem {Value = "1", Text = "Perdate"});
            pricetypes.Add(new SelectListItem {Value = "2", Text = "Perweek"});
            pricetypes.Add(new SelectListItem {Value = "3", Text = "Permonth"});
            ViewBag.pricetypes = pricetypes;

            ////Get Homestay type
            HomeStayTypeModel homestaytype = new HomeStayTypeModel();
            List<HomeStayType> homestaytypes = homestaytype.GetHomeStayType();
            List<SelectListItem> checklist = new List<SelectListItem>();
            foreach (var item in homestaytypes)
            {
                checklist.Add(new SelectListItem
                                  {
                                      Text = item.Name,
                                      Value = item.HomeStayTypeID.ToString()
                                  });




            }
            ViewBag.HomeStayTypeID = checklist;
            //set default min price
            searchBo.MinPrice = 1;
            searchBo.MaxPrice = 501;
            searchBo.MinArea = 0;
            searchBo.MaxArea = 201;
            ViewBag.Homepagepass = "SearchMap";
            ViewBag.txtSearch = txtsearch;
            HttpContext.Session["txtSearchTemp"] = txtsearch;
            if (txtsearch == null)
            {
                ViewBag.InitPage = "first"; //check chay index
            }
            //get provice district display search text
            if (provinceId != null)
            {
                searchBo.ProvinceID = int.Parse(provinceId.ToString());
                List<Region> listdistrict = model.SelectDistrict(searchBo.ProvinceID);
                List<SelectListItem> listitem3 = new List<SelectListItem>();
                if (listdistrict.Count > 0)
                {
                    listitem3.Add(new SelectListItem
                                      {
                                          Text = "All District",
                                          Value = "-1"
                                      });
                    foreach (var dis in listdistrict)
                    {

                        listitem3.Add(new SelectListItem
                                          {
                                              Text = dis.Name,
                                              Value = dis.RegionID.ToString()
                                          });
                    }

                }
                else
                {
                    listitem3.Add(new SelectListItem
                                      {
                                          Text = "Select District",
                                          Value = "-1"
                                      });

                }
                ViewBag.District = listitem3;
                if (districtId != null)
                {
                    searchBo.DistrictID = districtId;
                }
                
            }
            return View(searchBo);
        }

        // [HttpPost]
        public ActionResult Search(FormCollection form)
        {
            SearchPostModel model = new SearchPostModel();
            SearchPostBO house = new SearchPostBO();
            if (form["Housetype"] != "")
            {
                house.Housetype = Int32.Parse(form["Housetype"]);
            }

            if (form["ProvinceID"] != null)
            {
                house.ProvinceID = Int32.Parse(form["ProvinceID"]);
            }
            if (form["DistrictID"] != null)
            {
                if (Int32.Parse(form["DistrictID"]) != -1)
                {
                    house.DistrictID = Int32.Parse(form["DistrictID"]);
                }
                else
                {
                    house.DistrictID = null;
                }
            }

            house.MinPrice = Int32.Parse(form["MinPrice"]);
            house.MaxPrice = Int32.Parse(form["MaxPrice"]);
            house.Pricetype = Int32.Parse(form["Pricetype"]);
            house.MinArea = Int32.Parse(form["MinArea"]);
            house.MaxArea = Int32.Parse(form["MaxArea"]);
            
            if (form["HasGara"] != null)
            {
                house.HasGara = ((form["HasGara"]).ToString() == "true") ? true : false;
            }
            if (form["HasGarden"] != null)
            {
                house.HasGarden = ((form["HasGarden"]).ToString() == "true") ? true : false;
            }
            if (form["HasKitchen"] != null)
            {
                house.HasKitchen = ((form["HasKitchen"]).ToString() == "true") ? true : false;
            }
            if (form.GetValues("HomeStayTypeID") != null)
            {
                house.HomestaytypeID = form.GetValues("HomeStayTypeID"); //mang homestaytypeID

            }
           
            
            houseSt = house;
            HttpContext.Session["txtSearch"] = null;
            HttpContext.Session["houseSt"] = house;
            List<ResultObject> ListSearchResult = model.SearchPost(house);
            listtemp = ListSearchResult;
            ////Thuc Hien dung tung step : step 1 --> step 2

            string id = null;
            int count = model.SetCountPaging(8, house);//Step1: Get so page 
            ListSearchResult = model.GetResultSearchtOfPaing(ref id);//Step 2: get data cho moi page

            //ViewBag.data = "abc";
            ViewBag.count = count;
            ViewBag.page = id;
            ViewBag.PriceType = house.Pricetype;//loc ket qua search
            return PartialView("Result", ListSearchResult);
        }


        SearchPostModel model = new SearchPostModel();
        public ActionResult Paging(string id)
        {
            int count = 0;
            List<ResultObject> ListSearchResult = new List<ResultObject>();
            //Thuc Hien dung tung step : step 1 --> step 2
            if (HttpContext.Session["txtSearch"] != null)
            {
                String txtSearch = HttpContext.Session["txtSearch"].ToString();
                count = model.SetCountPagingText(8, txtSearch);//Step1: Get so page 
                ListSearchResult = model.GetResultSearchtTextOfPaing(ref id);//Step 2: get data cho moi page

            }
            else if (HttpContext.Session["houseSt"] != null)
            {
                SearchPostBO postBO = (SearchPostBO)HttpContext.Session["houseSt"];
                ViewBag.PriceType = postBO.Pricetype;//loc ket qua search
                //set session for district
                count = model.SetCountPaging(8, postBO);//Step1: Get so page 
                ListSearchResult = model.GetResultSearchtOfPaing(ref id);//Step 2: get data cho moi page
            }


            //ViewBag.data = "abc";
            ViewBag.count = count;
            ViewBag.page = id;
           

            if (Request.IsAjaxRequest())
            {
                return PartialView("Result", ListSearchResult);
            }
            return PartialView("Result", ListSearchResult);

        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SearchText(string txtSearch)
        {
            List<ResultObject> ListSearchResult = new List<ResultObject>();
            if (HttpContext.Session["txtSearchTemp"] != null)
            {
                txtSearch = HttpContext.Session["txtSearchTemp"].ToString();
            }
            if (txtSearch != null)
            {
                HttpContext.Session["houseSt"] = null;
                HttpContext.Session["txtSearch"] = txtSearch;
                SearchPostModel model = new SearchPostModel();
                ListSearchResult = model.SearchText(txtSearch);
                var provincedistricID = model.GetProDisId(txtSearch);
                int? provinceId = provincedistricID.ProvinceID;
                int? districtId = provincedistricID.DistrictID;
                HttpContext.Session["txtSearchTemp"] = null;
                listtemp = ListSearchResult;
                ////Thuc Hien dung tung step : step 1 --> step 2
                string id = null;
                int count = model.SetCountPagingText(8, txtSearch); //Step1: Get so page 
                ListSearchResult = model.GetResultSearchtTextOfPaing(ref id); //Step 2: get data cho moi page

                //ngocht
                ViewBag.ProvinceSearch = provinceId;
                ViewBag.DistrictSearch = districtId;
                
                ViewBag.count = count;
                ViewBag.page = id;
               return PartialView("Result", ListSearchResult);
               
                
            }
            else
            {
                return View("Index", new { province = 0, district = 0, txtsearch = string.Empty });
            }
        }

        public List<ResultObject> listtemp { get; set; }
        public  ActionResult SelectTopNew()
        {
            PostModel postmodel = new PostModel();
            ViewBag.TopPostNew = postmodel.ListTopPostNew();
            return PartialView("Result", ViewBag.TopPostNew);
        }

    }
}
